<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/17/017
 * Time: 12:05
 */
namespace App\Http\Common\Lib;
class Common
{
    public static function jsonFormat($status=200,$msg='OK',$data=null){
            $returnData = [
              'status'=>$status,
              'msg'=>$msg,
              'data'=>$data,
            ];
            echo json_encode($returnData);
    }

    /**
     * 格式化返回数据
     * @param int $status
     * @param string $msg
     * @param null $data
     * @return array
     */
    public static function dataFormat($status=200,$msg='OK',$data=null){
        return [
            'status'=>$status,
            'msg'=>$msg,
            'data'=>$data,
        ];
    }

    /**
     * curl
     * @param $url
     * @param null $params
     * @param string $type
     * @param int $timeout
     * @return mixed
     */
    public static function doCurl($url,$params=null,$type='get',$timeout=10){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        if(strtolower($type) == 'post'){
            curl_setopt($ch, CURLOPT_POST, 1);
            if($params != null){
                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            }
        }
        $contents = curl_exec($ch);
        curl_close($ch);
        return $contents;
    }

    /**
     * 获取客户端ip
     * @return bool
     */
    public static function getIp(){
        $ip=false;
        if(!empty($_SERVER["HTTP_CLIENT_IP"])){
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
            if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
            for ($i = 0; $i < count($ips); $i++) {
                if (!preg_match ("^(10│172.16│192.168).", $ips[$i])) {
                    $ip = $ips[$i];
                    break;
                }
            }
        }
        return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
    }

    /**
     * 通过ip获取地址信息
     * @param $ip
     * @return array
     */
    public static function ipToAddress($ip){
        $url = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
        $res = self::doCurl($url);
        if($res){
            $res = json_decode($res,true);
            if($res['code'] == 0){
                return self::dataFormat(config('statusInfo.status.success'),config('statusInfo.msg.success'),$res['data']);
            }
        }
        return self::dataFormat(config('statusInfo.status.fail'),config('statusInfo.msg.fail'));
    }

    /**
     * 创建随机字符串
     * @param int $length
     * @return bool|string
     */
    public static function randomString($length=6){
        $str = '1234567890-=_+qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*><';
        if($length > strlen($str)){
            $length = strlen($length);
        }
        return substr(str_shuffle($str),mt_rand(0,strlen($str)-$length-1),$length);
    }
}